package com.dly.hope.utils;

import com.dly.hope.constant.RedisConstant;
import com.dly.hope.webSocket.dto.MessageDto;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author linyu.dai
 * @project myserve
 * @description redis工具类
 * @creat 2022/11/6 15:15:34
 */
@Component
public class RedisUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public void setStr(String key, String value){
        redisTemplate.opsForValue().set(key,value);
    }

    public void setStr(String key, String value, long timeout, TimeUnit unit){
        redisTemplate.opsForValue().set(key,value,timeout,unit);
    }

    public void removeStr(String key){
        redisTemplate.delete(key);
    }

    /**
     * 判断该键是否存在
     */
    public Boolean exists(String key) {
        return redisTemplate.hasKey(key);
    }

    public String getStr(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    /**
     * @return void
     * @Author linyu.dai
     * @Description 添加set
     * @Date 2022/11/7 13:11
     * @Param [key, value]
     */
    public void addSet(String key, Object value) {
        redisTemplate.opsForSet().add(key,value);
    }

    /**
     * @return void
     * @Author linyu.dai
     * @Description 删除set
     * @Date 2022/11/7 13:11
     * @Param [key, value]
     */
    public void removeSet(String key, Object value){
        redisTemplate.opsForSet().remove(key,value);
    }

    /**
     * @return void
     * @Author linyu.dai
     * @Description 获取个数
     * @Date 2022/11/7 13:11
     * @Param [key, value]
     */
    public Long getSetSize(String key){
        return redisTemplate.opsForSet().size(key);
    }

    public Set<Object> getOnline(String key){
        return redisTemplate.opsForSet().members(key);
    }

    public Set<Object> getRing(String key){
        return redisTemplate.opsForSet().members(key);
    }

    /**
     * 订阅发布推送消息
     */
    public void push(String channel, String message){
        redisTemplate.convertAndSend(channel, message);
    }
}
